package com.kunbo.cn.service.impl.core.purchase;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.session.SessionProperties.Jdbc;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.kunbo.cn.dao.core.purchase.OrderWayBillDao;
import com.kunbo.cn.entity.core.purchase.OrderWayBill;
import com.kunbo.cn.service.core.purchase.OrderWayBillService;

/** 
* @ClassName:：OrderWayBillImpl 
* @Description： TODO
* @author ： Mr.liu
* @date ：2019年3月25日 下午2:28:11 
*  
*/
@Service
public class OrderWayBillImpl implements OrderWayBillService{
	
	@Autowired
	private OrderWayBillDao dao;
	@Autowired
	private JdbcTemplate jdbc;
	
	

	/* (non-Javadoc)
	 * @see com.kunbo.cn.service.core.purchase.OrderWayBillService#saveWayBillApp(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.Float, java.lang.String, java.lang.String, java.lang.Float, java.lang.Float, java.lang.String)
	 */
	@Override
	public OrderWayBill saveWayBillApp(String uuid,String create_user_id,String wayBillCode, String source, String material,
			Float order_quantity_to_be_sent, String supplier, String purchasing_unit,
			Float purchase_waybill_freightvolume, Float purchase_waybill_stayfreightvolume,
			String purchase_waybill_entrustedunit) {
		// TODO Auto-generated method stub
		OrderWayBill waybill = new OrderWayBill();
		waybill.setUuid(uuid);
		waybill.setCreate_user_id(create_user_id);
		waybill.setPurchase_waybill_code(wayBillCode);
		waybill.setPurchase_waybill_source(source);
		waybill.setPurchase_waybill_materiel(material);
		waybill.setOrder_quantity_to_be_sent(order_quantity_to_be_sent);
		waybill.setSupplier(supplier);
		waybill.setPurchasing_unit(purchasing_unit);
		waybill.setPurchase_waybill_freightvolume(purchase_waybill_freightvolume);
		waybill.setPurchase_waybill_stayfreightvolume(purchase_waybill_stayfreightvolume);
		waybill.setPurchase_waybill_entrustedunit(purchase_waybill_entrustedunit);
		waybill.setPurchase_waybill_state(4+"");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		waybill.setPurchase_waybill_time(format.format(new Date()));
		waybill = dao.saveAndFlush(waybill);
		if(waybill != null) {
			/*String selOrder = "select allowance from bussiness_purchasing_order_manager where purchase_order_code = '" + source + "'";
			Map<String,Object> map = jdbc.queryForMap(selOrder);
			if(map != null) {
				float allowance = Float.parseFloat(map.get("allowance").toString());
				if(allowance > 0 ) {
					String updateOrderSql = "update bussiness_purchasing_order_manager set allowance = allowance - " +  purchase_waybill_freightvolume + " where purchase_order_code = '" + source + "'";
					jdbc.update(updateOrderSql);
				}
			}*/
			return waybill;
		}else {
			return null;
		}
	}



	@Override
	public List<Map<String, Object>> selOrderWaybill(String supplier, Integer page, Integer pageSize) {
		// TODO Auto-generated method stub
		
		String sql = "select * from business_purchase_waybill where 1 = 1 ";
		if(supplier != null && !supplier.isEmpty()) {
			String suppliers = "select name from business_supplier_manage where id = " + Integer.parseInt(supplier);
			Map<String,Object> map = jdbc.queryForMap(suppliers);
			if(map.size() > 0 ) {
			sql += " and supplier  = '" + map.get("name").toString() + "'";
			}
		}
		sql += " order by id desc limit " + (page*pageSize) + "," + pageSize;
		return jdbc.queryForList(sql);
	}
}
